package com.pj.wisdomlogistics.ui.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhang on 2017/3/28.
 */

public class GuidePagerAdapter extends PagerAdapter {

    private List<Integer> mImages;
    private List<ImageView> mCache = new ArrayList();

    public GuidePagerAdapter(List<Integer> images) {
        this.mImages = images;
    }

    private int getItem(int position){
        return mImages.get(position);
    }

    @Override
    public int getCount() {
        return mImages == null ? 0: mImages.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mCache.size()==0){
            ImageView imageView = new ImageView(container.getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT ,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            mCache.add(imageView);
        }
        ImageView iv = mCache.remove(0);
        iv.setImageResource(getItem(position));
        container.addView(iv);
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ImageView iv = (ImageView) object;
        mCache.add(iv);
        container.removeView(iv);
    }
}
